<?php
/**
 * Created by PhpStorm.
 * User: lir08
 * Date: 2019/10/29
 * Time: 18:05
 */

namespace business\elasticsearch\repositories\upgrade;

use business\elasticsearch\models\upgrade\LandProjectModel;
use business\elasticsearch\repositories\EsQuery;
use business\elasticsearch\types\upgrade\LandProjectMapConfigType;

class LandProjectRepository
{
    /**
     * 创建拿地数据索引
     * @return mixed
     */
    public function createIndex()
    {
        return LandProjectModel::getDb()
            ->createCommand()
            ->createIndex(
                LandProjectModel::index(),
                [
                    'settings' => [
                        'index' => [
                            'number_of_shards' => 5,
                            'number_of_replicas' => 1,
                            'analysis.analyzer.default.type' => 'ik_max_word',
                        ]
                    ],
                    'mappings' => [
                        LandProjectModel::type() => LandProjectMapConfigType::mapConfig()
                    ]
                ]
            );
    }

    /**
     * 保存项目数据记录
     * @param array $data
     * @param int $id
     * @return array|mixed
     */
    public function saveRecord
    (
        $data = [],
        $id = 0,
        $options = ['routing' => 'land_project']
    ) {
        if (!empty($data)) {
            return LandProjectModel::getDb()
                ->createCommand()
                ->insert(LandProjectModel::index(), LandProjectModel::type(), $data, $id, $options);
        }

        return [];
    }

    /**
     * 查询项目数据记录
     * @param array $condition
     * @param array $include
     * @param array $aggs
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryList(
        $condition = [],
        $include = [],
        $aggs = [],
        $from = 0,
        $size = 0,
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandProjectModel::index(), LandProjectModel::type())
            ->options(['routing' => 'land_project'])
            ->query($condition);

        if (!empty($aggs)) {
            foreach ($aggs as $agg) {
                $query->addAggs($agg['name'], $agg['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [
            $query->count(),
            !empty($aggs)
                ? $query->createCommand()->search()['aggregations']
                : $query->all()
        ];
    }

    /**
     * 返回查询项目数据记录数量
     * @param array $condition
     * @param array $aggregations
     * @return int
     */
    public function queryCount($condition = [], $aggregations = [])
    {
        $query = (new EsQuery())
            ->from(LandProjectModel::index(), LandProjectModel::type())
            ->options(['routing' => 'land_project'])
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        return (int)$query
            ->createCommand()
            ->search()['aggregations']['count']['value'];
    }
}
